using HKB.Common;
using HKB.Data.Model;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;
namespace HKB.Data.Dal
{
	public class CalculateUnit
	{
		public int GetMaxId()
		{
			return Conn.GetMaxID("CalculateUnitID", "HLJXC_CalculateUnit");
		}
		public bool Exists(int CalculateUnitID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) from HLJXC_CalculateUnit");
			stringBuilder.Append(" where CalculateUnitID=@CalculateUnitID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CalculateUnitID", SqlDbType.Int, 4)
			};
			array[0].Value = CalculateUnitID;
			return Conn.Exists(stringBuilder.ToString(), array);
		}
		public int Add(HKB.Data.Model.CalculateUnit model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("insert into HLJXC_CalculateUnit(");
			stringBuilder.Append("CalculateUnitNumber,CalculateUnitName,Remark)");
			stringBuilder.Append(" values (");
			stringBuilder.Append("@CalculateUnitNumber,@CalculateUnitName,@Remark)");
			stringBuilder.Append(";select @@IDENTITY");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CalculateUnitNumber", SqlDbType.NVarChar, 50),
				new SqlParameter("@CalculateUnitName", SqlDbType.NVarChar, 50),
				new SqlParameter("@Remark", SqlDbType.NVarChar, 255)
			};
			array[0].Value = model.CalculateUnitNumber;
			array[1].Value = model.CalculateUnitName;
			array[2].Value = model.Remark;
			object single = Conn.GetSingle(stringBuilder.ToString(), array);
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public bool Update(HKB.Data.Model.CalculateUnit model)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("update HLJXC_CalculateUnit set ");
			stringBuilder.Append("CalculateUnitNumber=@CalculateUnitNumber,");
			stringBuilder.Append("CalculateUnitName=@CalculateUnitName,");
			stringBuilder.Append("Remark=@Remark");
			stringBuilder.Append(" where CalculateUnitID=@CalculateUnitID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CalculateUnitNumber", SqlDbType.NVarChar, 50),
				new SqlParameter("@CalculateUnitName", SqlDbType.NVarChar, 50),
				new SqlParameter("@Remark", SqlDbType.NVarChar, 255),
				new SqlParameter("@CalculateUnitID", SqlDbType.Int, 4)
			};
			array[0].Value = model.CalculateUnitNumber;
			array[1].Value = model.CalculateUnitName;
			array[2].Value = model.Remark;
			array[3].Value = model.CalculateUnitID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool Delete(int CalculateUnitID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HLJXC_CalculateUnit ");
			stringBuilder.Append(" where CalculateUnitID=@CalculateUnitID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CalculateUnitID", SqlDbType.Int, 4)
			};
			array[0].Value = CalculateUnitID;
			int num = Conn.ExecuteSql(stringBuilder.ToString(), array);
			return num > 0;
		}
		public bool DeleteList(string CalculateUnitIDlist)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("delete from HLJXC_CalculateUnit ");
			stringBuilder.Append(" where CalculateUnitID in (" + CalculateUnitIDlist + ")  ");
			int num = Conn.ExecuteSql(stringBuilder.ToString(), new IDbDataParameter[0]);
			return num > 0;
		}
		public HKB.Data.Model.CalculateUnit GetModel(int CalculateUnitID)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select  top 1 CalculateUnitID,CalculateUnitNumber,CalculateUnitName,Remark from HLJXC_CalculateUnit ");
			stringBuilder.Append(" where CalculateUnitID=@CalculateUnitID");
			SqlParameter[] array = new SqlParameter[]
			{
				new SqlParameter("@CalculateUnitID", SqlDbType.Int, 4)
			};
			array[0].Value = CalculateUnitID;
			HKB.Data.Model.CalculateUnit calculateUnit = new HKB.Data.Model.CalculateUnit();
			DataSet dataSet = Conn.Query(stringBuilder.ToString(), array);
			HKB.Data.Model.CalculateUnit result;
			if (dataSet.Tables[0].Rows.Count > 0)
			{
				result = this.DataRowToModel(dataSet.Tables[0].Rows[0]);
			}
			else
			{
				result = null;
			}
			return result;
		}
		public HKB.Data.Model.CalculateUnit DataRowToModel(DataRow row)
		{
			HKB.Data.Model.CalculateUnit calculateUnit = new HKB.Data.Model.CalculateUnit();
			if (row != null)
			{
				if (row["CalculateUnitID"] != null && row["CalculateUnitID"].ToString() != "")
				{
					calculateUnit.CalculateUnitID = int.Parse(row["CalculateUnitID"].ToString());
				}
				if (row["CalculateUnitNumber"] != null)
				{
					calculateUnit.CalculateUnitNumber = row["CalculateUnitNumber"].ToString();
				}
				if (row["CalculateUnitName"] != null)
				{
					calculateUnit.CalculateUnitName = row["CalculateUnitName"].ToString();
				}
				if (row["Remark"] != null)
				{
					calculateUnit.Remark = row["Remark"].ToString();
				}
			}
			return calculateUnit;
		}
		public DataSet GetList(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select CalculateUnitID,CalculateUnitNumber,CalculateUnitName,Remark ");
			stringBuilder.Append(" FROM HLJXC_CalculateUnit ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select ");
			if (Top > 0)
			{
				stringBuilder.Append(" top " + Top.ToString());
			}
			stringBuilder.Append(" CalculateUnitID,CalculateUnitNumber,CalculateUnitName,Remark ");
			stringBuilder.Append(" FROM HLJXC_CalculateUnit ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			stringBuilder.Append(" order by " + filedOrder);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
		public int GetRecordCount(string strWhere)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("select count(1) FROM HLJXC_CalculateUnit ");
			if (strWhere.Trim() != "")
			{
				stringBuilder.Append(" where " + strWhere);
			}
			object single = DbHelperSQL.GetSingle(stringBuilder.ToString());
			int result;
			if (single == null)
			{
				result = 0;
			}
			else
			{
				result = System.Convert.ToInt32(single);
			}
			return result;
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			System.Text.StringBuilder stringBuilder = new System.Text.StringBuilder();
			stringBuilder.Append("SELECT * FROM ( ");
			stringBuilder.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				stringBuilder.Append("order by T." + orderby);
			}
			else
			{
				stringBuilder.Append("order by T.CalculateUnitID desc");
			}
			stringBuilder.Append(")AS Row, T.*  from HLJXC_CalculateUnit T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				stringBuilder.Append(" WHERE " + strWhere);
			}
			stringBuilder.Append(" ) TT");
			stringBuilder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return Conn.Query(stringBuilder.ToString(), new IDbDataParameter[0]);
		}
	}
}
